package edu.gatech.ece6102;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketException;

/**
 * User: deepal
 * Date: Nov 14, 2010
 * Time: 7:21:39 PM
 */
public class Logtester {
    public static void main2(String[] args) throws Exception {
        Socket logger = new Socket("node1.faulttolerance.infosphere.emulab.net", 2201);
        OutputStream out = logger.getOutputStream();
        InputStream in = logger.getInputStream();
        String message = "\"C:\\Program Files\\Java\\jdk1.6.0_21\\bin\\java\" -Didea.launcher.port=7582 \"-Didea.launcher.bin.path=C:\\Program Files\\JetBrains\\IntelliJ IDEA 9.0.2\\bin\" -Dfile.encoding=windows-1252 -classpath \"C:\\Program Files\\Java\\jdk1.6.0_21\\jre\\lib\\alt-rt.jar;C:\\Program Files\\Java\\jdk1.6.0_21\\jre\\lib\\charsets.jar;C:\\Program Files\\Java\\jdk1.6.0_21\\jre\\lib\\deploy.jar;C:\\Program Files\\Java\\jdk1.6.0_21\\jre\\lib\\javaws.jar;C:\\Program Files\\Java\\jdk1.6.0_21\\jre\\lib\\jce.jar;C:\\Program Files\\Java\\jdk1.6.0_21\\jre\\lib\\jsse.jar;C:\\Program Files\\Java\\jdk1.6.0_21\\jre\\lib\\management-agent.jar;C:\\Program Files\\Java\\jdk1.6.0_21\\jre\\lib\\plugin.jar;C:\\Program Files\\Java\\jdk1.6.0_21\\jre\\lib\\resources.jar;C:\\Program Files\\Java\\jdk1.6.0_21\\jre\\lib\\rt.jar;C:\\Program Files\\Java\\jdk1.6.0_21\\jre\\lib\\ext\\dnsns.jar;C:\\Program Files\\Java\\jdk1.6.0_21\\jre\\lib\\ext\\localedata.jar;C:\\Program Files\\Java\\jdk1.6.0_21\\jre\\lib\\ext\\sunjce_provider.jar;C:\\Program Files\\Java\\jdk1.6.0_21\\jre\\lib\\ext\\sunmscapi.jar;C:\\Program Files\\Java\\jdk1.6.0_21\\jre\\lib\\ext\\sunpkcs11.jar;D:\\academics\\fall10\\ECE6102\\eweb\\out\\production\\eweb;C:\\Users\\deepal\\.m2\\repository\\commons-logging\\commons-logging-api\\1.0.4\\commons-logging-api-1.0.4.jar;C:\\Users\\deepal\\.m2\\repository\\commons-logging\\commons-logging\\1.1.1\\commons-logging-1.1.1.jar;C:\\Users\\deepal\\.m2\\repository\\junit\\junit\\4.5\\junit-4.5.jar;C:\\Program Files\\JetBrains\\IntelliJ IDEA 9.0.2\\lib\\idea_rt.jar\" com.intellij.rt.execution.application.AppMain edu.gatech.ece6102.Logtester";
        int length = message.length();
        String str = "P0000";
        if (length < 10) {
            str = str + "000" + length;
        } else if (length < 100) {
            str = str + "00" + length;
        } else if (length < 1000) {
            str = str + "0" + length;
        } else {
            str = str + length;
        }
        String tol = str + message;
        byte[] bt = tol.getBytes();
        System.out.println(new String(bt));
        out.write(bt);
        out.flush();
        byte[] buf = new byte[2048];
        int n;
        try {
            System.out.println("before");
            while ((n = in.read(buf)) < 0) {
            }

            String key = new String(buf, 1, n);
            System.out.println("key = " + key);
        } catch (SocketException se) {
            if (se.getMessage().startsWith("Connection reset by peer")) {
                System.out.println("Found error on Tomcat");
            }
        }
    }

    public static void main(String[] args) throws Exception{
//        NetworkLogger logger = new NetworkLogger("karp.cc.gt.atl.ga.us", 2201);
        NetworkLogger logger = new NetworkLogger("node1.faulttolerance.infosphere.emulab.net", 2201);
        String message = "message12345";
        String key = logger.store(message.getBytes());
        System.out.println("key = " + key);
        byte[] bt = logger.retrieve(key);
        System.out.println("bt = " + new String(bt));
        logger.delete(key);
        bt = logger.retrieve(key);
        System.out.println("bt = " + new String(bt));
    }


}
